package org.iabako.server.mail.template;

import org.iabako.server.tools.BundleMessages;
import org.iabako.shared.entity.business.Tracking;
import org.iabako.shared.entity.user.Enterprise;
import org.iabako.shared.entity.user.User;
import org.iabako.shared.tools.GenericTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by jose on 22/05/15.
 */
@Component
public class TrackingEmailTemplate extends IabakoMailTemplate {

    private final String tableTracking = "<center><div style='text-align:left; position:relative; max-width: 400px; width:50%; background-color: #f5f5f5; margin: 5px auto; display: table;'>_content_</div></center>";
    private final String rowDiv = "<div style='border-bottom:2px solid white; display: table-row; width: 100%;'>";
    private final String cardinalDiv = "<div style='border-bottom:4px solid white; display: table-cell; color:#0088CC; font-size: 20px; width: 50px; text-align: center; vertical-align:middle; padding: 5px 0px;'>";
    private final String textDiv = "<div style='border-bottom:4px solid white; display: table-cell; vertical-align:middle; font-size: 14px; padding: 5px'>";

    @Autowired
    BundleMessages messages;

    private Enterprise enterprise;

    public TrackingEmailTemplate getInstance(Enterprise e, User user, List<Tracking> trackings) {

        if (trackings == null || trackings.isEmpty()) {
            return null;
        }

        enterprise = e;
        List<Tracking> trackingToSend = new ArrayList<Tracking>();

        Date dateOfTracking = trackings.get(0).getDate();

        for (Tracking tracking : trackings) {
            if (user.equals(tracking.getUser())) {
                continue;
            }
            trackingToSend.add(tracking);
        }

        if(trackingToSend.isEmpty()){
            return null;
        }

        TrackingEmailTemplate template = new TrackingEmailTemplate();
        //Welcome
        String msg = messages.getLabel(enterprise.getLanguage(), "tracking_email_welcome", user.getFirstName(), enterprise.getName(), GenericTools.formatDate(dateOfTracking));
        template.textBody = msg.replace("<b>", "").replace("</b>", "");
        template.htmlBody = msg;
        template.textBody += getTrackingListAsString(trackingToSend);
        template.htmlBody += getTrackingListAsHtml(trackingToSend);
        //Greeting
        msg = messages.getLabel(enterprise.getLanguage(), "tracking_email_greeting");
        template.textBody += msg;
        template.htmlBody += msg;

        template.htmlBody = "<div style='font-size: 13px'>" + template.htmlBody + "</div>";
        return template;
    }

    private String getTrackingListAsString(List<Tracking> trackingList) {
        String text = "\n\n";
        for (Tracking tracking : trackingList) {
            String lineText = messages.getLabel(enterprise.getLanguage(), tracking.getType().getLabelKey(), tracking.getUser().getName());
            text += "- " + tracking.getCardinal() + " " + lineText + "\n";
        }
        return text;
    }

    private String getTrackingListAsHtml(List<Tracking> trackingList) {
        String htmlText = "<br/><br/>" + tableTracking;
        String content = "";
        for (Tracking tracking : trackingList) {
            String line = rowDiv + cardinalDiv + "_cardinal_</div>" + textDiv + "_text_</div></div>";
            String lineText = messages.getLabel(enterprise.getLanguage(), tracking.getType().getLabelKey(), "<span style='color:#99CC00'>" + tracking.getUser().getName() + "</span>");
            content += line.replace("_cardinal_", tracking.getCardinal() + "").replace("_text_", lineText);
        }
        return htmlText.replace("_content_", content);
    }


    @Override
    public String getHtmlBody() {
        return htmlBody.replace("\n", "<br/>");
    }

    @Override
    public String getTextBody() {
        return textBody;
    }
}
